package com.ruoyi.prod.controller;

import com.github.pagehelper.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.prod.entity.dto.ProdAssetSummary;
import com.ruoyi.prod.entity.po.ProdAsset;
import com.ruoyi.prod.entity.query.ProdAssetCondition;
import com.ruoyi.prod.entity.query.ProdAssetEditCondition;
import com.ruoyi.prod.enums.AssetState;
import com.ruoyi.prod.service.ProdAssetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 资产管理-资产台账 前端控制器
 * </p>
 *
 * @author 强子初
 * @since 2023-02-18
 */
@Api(tags = "资产管理-资产台账")
@RestController
@RequestMapping("/prod/asset/account")
public class ProdAssetController {

    @Autowired
    ProdAssetService baseService;

    /**
     * 详情
     */
    @ApiOperation("详情")
    @GetMapping("/{id}")
    public R<ProdAsset> getInfo(@PathVariable String id) {
        return R.ok(baseService.getById(id));
    }

    /**
     * 列表
     */
    @ApiOperation("列表")
    @PostMapping("/list")
    public R<List<ProdAsset>> list(@Validated @RequestBody ProdAsset condition) {
        return R.ok(baseService.list(condition));
    }

    /**
     * 分页
     */
    @ApiOperation("分页")
    @PostMapping("/page")
    public R<List<ProdAsset>> page(@Validated @RequestBody ProdAsset condition) {
        Page<ProdAsset> page = baseService.page(condition);
        return R.page(page.getResult(), page.toPageInfo());
    }

    /**
     * 汇总
     */
    @ApiOperation("汇总")
    @PostMapping("/summary")
    public R<List<ProdAssetSummary>> summary(@Validated @RequestBody ProdAssetCondition condition) {
        Page<ProdAssetSummary> page = baseService.summary(condition);
        return R.page(page.getResult(), page.toPageInfo());
    }

    @ApiOperation("资产归还")
    @PostMapping("/edit")
    public R<?> edit(@Validated @RequestBody ProdAssetEditCondition condition) {

        // 默认新资产状态未使用
        if(condition.getAssetState() == null) {
            condition.setAssetState(AssetState.NOT_USED);
        }

        baseService.updateToAssetState(condition.getId(), condition.getAssetState());
        return R.ok("状态修改成功");
    }
}